//
//  CategoryView.m
//  EnjoyLife
//
//  Created by ihope-UI on 15/10/12.
//  Copyright (c) 2015年 Hepburn. All rights reserved.
//

#import "ALLButtonView.h"


@interface ALLButtonView ()

/**
 *保存button的数组
 */
@property(nonatomic,strong)NSMutableArray *arrayWithButton;

@property(nonatomic,strong)UIImageView *lineView;

/**
 *选中状态的button
 */
@property(nonatomic,strong)UIButton *selectButton;


@end

@implementation ALLButtonView{
    //水平方向的坐标
    CGFloat _horX;
    //水平方向的间距
    CGFloat _horSpacingOfButtons;
    //滚动视图
    UIScrollView *_scollView;
    
    CGFloat _buttonW;
    NSDictionary *titleDict;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        self.arrayWithButton = [NSMutableArray array];
        self.backgroundColor = [UIColor whiteColor];
        // buttons 水平间距
        _horSpacingOfButtons = 20.f;
        _scollView  = [[UIScrollView alloc]initWithFrame:self.bounds];
        _scollView.backgroundColor = [UIColor clearColor];
        //去掉水平滚动条
        _scollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_scollView];
        
        [self initLineView];
    }
    return self;
}

-(void)setMTitleArray:(NSArray *)mTitleArray{
    _mTitleArray = mTitleArray;
    for (NSInteger i = 0; i < mTitleArray.count; i ++ ) {
        [self loadButtnWithTitle:mTitleArray[i]];
    }
    [self loadbuttonEnd];
}


//View 下划线 50@2x_13

-(void)loadButtnWithTitle:(id)sender{
    
    NSString *title =(NSString *)sender;
    
    UIFont *font = [UIFont systemFontOfSize:14.];
    
    CGRect rect=  [title boundingRectWithSize:CGSizeMake(1000, self.frame.size.height) options:0 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.]} context:NULL];
    // button 的宽度
    CGFloat buttonWidth = rect.size.width;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    if (_horX == 0.){
        _horX += 15;
    }
    else{
        _horX += _horSpacingOfButtons;
    }
    
    // Frame
    [button setFrame:CGRectMake(_horX, 0, buttonWidth, self.frame.size.height)];
    //title
    [button setTitle:title forState:UIControlStateNormal];
    //Font
    [button.titleLabel setFont:font];
    [button setTitleColor:KBorColor forState:UIControlStateNormal];
    [button setTitleColor:KTinColor forState:UIControlStateSelected];
    
    [button addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_scollView addSubview:button];
    [self.arrayWithButton addObject:button];
    

    _buttonW = buttonWidth;
    
    //记录水平防线的最后坐标
    _horX += buttonWidth;


}

/**
 *加载完button 之后调用的方法
 */
-(void)loadbuttonEnd{
    _scollView.contentSize = CGSizeMake(_horX + 15. ,40);
    //默认 选中第一个
    self.selectButton = self.arrayWithButton[0];
//    [self clickButtonAction:self.selectButton];
}

-(void)clickButtonAction:(UIButton *)button{
    self.selectButton = button;
    
    NSInteger index = [self.arrayWithButton indexOfObject:button];

    if (self.topButtonActionBlock) {
        self.topButtonActionBlock(index);
    }    
    // 滑动到一定范围 自动滑到中间
    [_scollView scrollRectToVisible:CGRectMake(self.selectButton.center.x  - _scollView.frame.size.width / 2., 0, _scollView.frame.size.width, _scollView.frame.size.height) animated:YES];
}



//滚动的时候调用
-(void)mainScrollViewDidSroll:(NSInteger )index;
{
    self.selectButton = self.arrayWithButton[index];
    
    // 滑动到一定范围 自动滑到中间
    [_scollView scrollRectToVisible:CGRectMake(self.selectButton.center.x  - _scollView.frame.size.width / 2., 0, _scollView.frame.size.width, _scollView.frame.size.height) animated:YES];

    [self clickButtonAction:self.selectButton];
}


-(void)initLineView{
    self.lineView = [[UIImageView alloc]initWithFrame:CGRectMake(15,35,15, 3)];
    self.lineView.backgroundColor = KTinColor;
    [_scollView addSubview:self.lineView];
}

#pragma mark getter
-(void)setSelectButton:(UIButton *)selectButton{
    // 不是 同一个  Button 时 去赋值
    if ([_selectButton isEqual:selectButton] == NO){
        if (_selectButton){
            // 上一次 选中Button 设为 正常状态
            _selectButton.selected = NO;
            _selectButton.titleLabel.font = [UIFont systemFontOfSize:14.];
            
        }
        // 记录这一次 点击的button
        _selectButton = selectButton;
        
        _selectButton.titleLabel.font = [UIFont systemFontOfSize:16.];
        
        [_selectButton setSelected:YES];
        

    }
    [UIView animateWithDuration:0.5 animations:^{
        
        self.lineView.frame = CGRectMake(_selectButton.frame.origin.x,35, _selectButton.frame.size.width,3);
    }];

    
}


@end
